package com.opages.mybatis;

import com.opages.mybatis.mapper.UserMapper;
import com.opages.mybatis.model.User;
import org.apache.ibatis.cache.decorators.LruCache;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException;

/**
 * @author daizhong.liu
 * @create 2021-11-14 16:42
 **/
public class MybatisMain {
    private static SqlSessionFactory sqlSessionFactory;

    public static SqlSession getSqlSession() throws FileNotFoundException {
        //配置文件
        InputStream configFile = new FileInputStream(
                "E:\\idea_workspace\\gitee\\opages-source\\mybatis\\src\\main\\resources\\mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configFile);
        //加载配置文件得到SqlSessionFactory
        return sqlSessionFactory.openSession();
    }

    public static SqlSessionFactory getSqlSessionFactory() throws FileNotFoundException {
        InputStream configFile = new FileInputStream("E:\\idea_workspace\\gitee\\opages-source\\mybatis\\src\\main\\resources\\mybatis-config.xml");
        if (null != sqlSessionFactory) {
            return sqlSessionFactory;
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(configFile);
        return sqlSessionFactory;
    }

    public static User get(SqlSession sqlSession, long id) throws SQLException {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        long start = System.currentTimeMillis();
        User user = userMapper.selectById(id,null);
        System.out.println("cost:" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        user = userMapper.selectById(id,null);
        System.out.println("cost:" + (System.currentTimeMillis() - start) + "\n" + user);
//        user.setName(String.valueOf(new Random().nextInt(5)));
//        userMapper.updateByPrimaryKey(user);
        start = System.currentTimeMillis();
        user = userMapper.selectById(id,null);
        System.out.println("cost:" + (System.currentTimeMillis() - start));
        return user;
    }

    public static User getOne(SqlSession sqlSession, long id) throws SQLException {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        long start = System.currentTimeMillis();
        User user = userMapper.selectById(id,null);
        System.out.println("cost:" + (System.currentTimeMillis() - start));
        return user;
    }



    public static long insert(SqlSession sqlSession, User user) {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        return userMapper.insert(user);
    }
    public void pluginTest() {

    }

    public static void main(String[] args) throws FileNotFoundException {
//        SqlSession sqlSession = getSqlSession();
//        try {
//            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////            User user = userMapper.selectById(1l,null);
////            System.out.println(user.toString());
////            User user1 = userMapper.selectById(1l, null);
////            System.err.println(user1.toString());
//            User user = userMapper.selectUserById(1l);
//            System.err.println(user.toString());
//            UserMapper userMapper2 = sqlSession.getMapper(UserMapper.class);
//            User user1 = userMapper2.selectUserById(1l);
//            System.err.println(user1.toString());
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            sqlSession.commit();
//            sqlSession.close();
//        }
//        new MybatisMain().testCache();

        new MybatisMain().testTwoCache();
    }
    public void testTwoCache() {
        try {
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            SqlSession sqlSession = sqlSessionFactory.openSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User user = mapper.selectById(1l, null);
            System.err.println(user.getUsername());
            user = mapper.selectById(1l, null);
            System.err.println("再次查询:"+user.getUsername());
            sqlSession.commit();
            SqlSession sqlSession1 = sqlSessionFactory.openSession();
            UserMapper mapper1 = sqlSession1.getMapper(UserMapper.class);
            User user1 = mapper1.selectById(1l, null);
            System.err.println("user1="+user1.getUsername());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void testCache() {
        LruCache lruCache = new LruCache(new PerpetualCache("test_lru"));
        lruCache.setSize(3);
        lruCache.putObject("1","1");
        lruCache.putObject("2","2");
        lruCache.putObject("3","3");
        lruCache.putObject("4","4");
        lruCache.putObject("5","5");
        lruCache.putObject("6","6");
        System.err.println(lruCache.getSize());
        System.err.println("over");
    }

    public static void diffSession() throws Exception {
        SqlSession sqlSession1 = getSqlSessionFactory().openSession();
        SqlSession sqlSession2 = getSqlSessionFactory().openSession();
        SqlSession sqlSession3 = getSqlSessionFactory().openSession();
        getOne(sqlSession1, 1);
        getOne(sqlSession1, 1);

        sqlSession1.commit();
        sqlSession1.close();
        getOne(sqlSession2, 1);
        sqlSession2.commit();
        sqlSession2.close();
        getOne(sqlSession3, 1);
        sqlSession3.commit();
        sqlSession3.close();
    }
}
